package serviceImpl;

import dao.ISeatDao;
import models.Seat;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 国彬 on 2016/7/10.
 */
public class SeatDaoImpl implements ISeatDao {


    @Override
    public List findByMovieTheatreId(String movieTheatreId) {
        List<Seat> seats = Seat.find("movieTheatreId=?", movieTheatreId).fetch();
        int[][] se = new int[10][];
        for(int i = 0;i<10;i++) {
            se[i] = new int[10];
            for (int j = 0; j < 10; j++) {
                se[i][j] = 1;
            }
        }
        for(Seat s:seats){
            if(s.isActive==0||s.isLock==1||s.isOpen==0){
                se[s.row-1][s.col-1]=0;
            }
        }
        List<String> s = new ArrayList<String>();
        for(int i =0;i<10;i++) {
            StringBuffer sf = new StringBuffer();
            for (int j = 0; j < 10; j++) {
                sf.append(se[i][j]);
            }
            s.add(sf.toString());
        }
        return s;
    }

    @Override
    public void lockSeat(String movieTheatreId, int col,int row) {
        Seat seat = Seat.find("movie_theatre_id=? and col=? and row=?",movieTheatreId,col,row).first();
        seat.isLock = 1;
        seat.save();
    }

    @Override
    public void bookSeat(String movieTheatreId, int col, int row) {
        Seat seat = Seat.find("movie_theatre_id=? and col=? and row=?",movieTheatreId,col,row).first();
        seat.isOpen = 1;
        seat.save();
    }
}
